import config from '@/config/config';

const responseHandle = function(response) {
  return new Promise(function(resolve, reject) {
    let data = response.data;
    if (data.code === 200) {
      resolve(data);
    } else {
      reject(data);
    }
  });
};

export default {
  async get(url, params) {
    let host = config.host || '';
    let [error, response] = await uni.request({
      method: 'GET',
      header: config.header(),
      url: host + url,
      data: params
    });
    if (error) {
      return Promise.reject(error);
    }
    return responseHandle(response);
  },
  async post(url, params) {
    let host = config.host || '';
    let [error, response] = await uni.request({
      method: 'POST',
      header: config.header(),
      url: host + url,
      data: params
    });
	console.log(error,response)
    if (error) {
      return Promise.reject(error);
    }
    return responseHandle(response);
  },
  async put(url, params) {
    let host = config.host || '';
    let [error, response] = await uni.request({
      method: 'PUT',
      header: config.header(),
      url: host + url,
      data: params
    });
    if (error) {
      return Promise.reject(error);
    }
    return responseHandle(response);
  },
  async delete(url, params) {
    let host = config.host || '';
    let [error, response] = await uni.request({
      method: 'DELETE',
      header: config.header(),
      url: host + url,
      data: params
    });
    if (error) {
      return Promise.reject(error);
    }
    return responseHandle(response);
  }
};
